define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'userapply/index',
                    table: 'c_user_apply',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                columns: [
                    [
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: false},
                        {field: 'uid', title: __('Uid'), operate: false},
                        {field: 'nickname', title: __('Nickname'), operate: false},
                        {field: 'type', title: __('Type'), searchList: {'1':__('申请加入'), '2': __('申请退出')}},
                        {field: 'operate', title: __('Operate'), table: table, formatter: function (value, row, index) {
                        	return '<a title="同意" class="btn btn-xs btn-success btn-agree" href="/club/userapply/agree/clubid/'+row.clubid+'/uid/'+row.uid+'/type/'+row.typetext+'"><i class=""></i> 同意</a>&nbsp;&nbsp;' 
                        	+ '<a title="拒绝" class="btn btn-xs btn-danger btn-agree" href="/club/userapply/refuse/clubid/'+row.clubid+'/uid/'+row.uid+'/type/'+row.typetext+'"><i class=""></i> 拒绝</a>';
						},events: Table.api.events.operate}
                        
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            
            //点击包含.btn-ajax的元素时发送Ajax请求
            $(document).on('click', '.btn-agree', function (e) {
                var options = $(this).data();
                if (typeof options.url === 'undefined' && $(this).attr("href")) {
                    options.url = $(this).attr("href");
                }
                $.post(options.url).success(function(data){
                    if (data.code==1) {
                        if (data.msg) {
                        	Layer.alert(data.msg);
                        }
                        table.bootstrapTable('refresh');
                    }else{
                    	Layer.alert(data.msg);
                    }
                });
                return false;
            });
            
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});